﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;

namespace QUAN_LY_MUON_TRA_SACH
{
    public partial class frmthemdocgia : Form
    {
        public frmthemdocgia()
        {
            InitializeComponent();
        }

        private void frmthemdocgia_Load(object sender, EventArgs e)
        {
            txtmathe.Focus();
            DataTable dbcbo_loaidg = new DataTable();
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select ma_loai_dgia,dgiai from loai_doc_gia",Connection.sqlConnection);
            sqlDataAdapter.Fill(dbcbo_loaidg);
            cboloaidg.DataSource = dbcbo_loaidg;
            cboloaidg.ValueMember = "ma_loai_dgia";

            sqlDataAdapter = new SqlDataAdapter("select ma_ng,ten_ng from nganh", Connection.sqlConnection);
            DataTable dbcbo_nganh = new DataTable();
            sqlDataAdapter.Fill(dbcbo_nganh);
            cbonganh.DataSource = dbcbo_nganh;
            cbonganh.ValueMember = "ma_ng";
            
            sqlDataAdapter = new SqlDataAdapter("select msnv,hten_nv from nhan_vien", Connection.sqlConnection);
            DataTable dbcbo_nhanvien = new DataTable();
            sqlDataAdapter.Fill(dbcbo_nhanvien);
            cbonhanvien.DataSource = dbcbo_nhanvien;
            cbonhanvien.ValueMember = "msnv";
            cbonhanvien.Text = FORM_MAIN.frmmain.barmsnv.Caption;
            cbonhanvien.Enabled = false;
            //tu dong sinh ma the
            SqlCommand lenh = new SqlCommand("select max(cast(substring(mathe,3,len(mathe)-2) as int))+1 from the_doc_gia",Connection.sqlConnection);
            if(Convert.ToInt32(lenh.ExecuteScalar())<100)
                txtmathe.Text = "DG00" + Convert.ToString(lenh.ExecuteScalar());
            else if (Convert.ToInt32(lenh.ExecuteScalar()) >= 100 && Convert.ToInt32(lenh.ExecuteScalar()) <1000)
                txtmathe.Text = "DG0" + Convert.ToString(lenh.ExecuteScalar());
            else
                txtmathe.Text = "DG" + Convert.ToString(lenh.ExecuteScalar());
            //khoi tao gia tri ban dau cho hop anh doc gia
            openanhdocgia.FileName = "";
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand lenh = new SqlCommand("insert into the_doc_gia values('" + txtmathe.Text+"'" + "," +"'"+ cbonhanvien.Text+"'" + "," +"'"+ cboloaidg.Text+"'" + "," +"'"+ cbonganh.Text +"'"+ "," +"N'"+ txthoten.Text+"'" + ","+"'" + cadngaysinh.Text+"'" + "," +"N'"+ cbogioitinh.Text+"'" + "," +"'"+ txtcmnd.Text+"'" + "," +"N'"+ txtdiachi.Text+"'" + "," +"'"+ txtdienthoai.Text+"'" + "," +"'"+ txtemail.Text+"'" + "," +"'"+ cadngaycap.Text +"'"+ "," +"'"+ cadnghethan.Text+"'" + "," +"N'"+ cbotrang.Text+"','" +openanhdocgia.FileName+ "')", Connection.sqlConnection);
                int n=lenh.ExecuteNonQuery();
                MessageBox.Show(n+" đọc giả mới đã được thêm!","Cập nhật thành công",MessageBoxButtons.OK,MessageBoxIcon.Information);
                lenh.Dispose();
                btninthe.Enabled = true;
                FORM_MAIN.frmmain.btndanhsach_Click(sender,e);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnnhaplai_Click(object sender, EventArgs e)
        {
            txtmathe.Clear();
            txthoten.Clear();
            cadngaysinh.Text = "";
            txtcmnd.Clear();
            cbogioitinh.Text = "";
            cboloaidg.Clear();
            cbonganh.Clear();
            txtdiachi.Clear();
            txtdienthoai.Clear();
            txtemail.Clear();
            cbotrang.Text = "";
            cadngaycap.Text="";
            cadnghethan.Text = "";
            ptbanhdg.Image = null;
            //tu dong sinh ma the moi
            SqlCommand lenh = new SqlCommand("select max(cast(substring(mathe,3,len(mathe)-2) as int))+1 from the_doc_gia", Connection.sqlConnection);
            if (Convert.ToInt32(lenh.ExecuteScalar()) < 100)
                txtmathe.Text = "DG00" + Convert.ToString(lenh.ExecuteScalar());
            else if (Convert.ToInt32(lenh.ExecuteScalar()) >= 100 && Convert.ToInt32(lenh.ExecuteScalar()) < 1000)
                txtmathe.Text = "DG0" + Convert.ToString(lenh.ExecuteScalar());
            else
                txtmathe.Text = "DG" + Convert.ToString(lenh.ExecuteScalar());
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btninthe_Click(object sender, EventArgs e)
        {
            rpt_the_doc_gia.mathe = txtmathe.Text;
            frmthe_doc_gia thedg = new frmthe_doc_gia();
            thedg.ShowDialog();
            btninthe.Enabled = false;
        }

        private void btnnapanh_Click(object sender, EventArgs e)
        {
            openanhdocgia.Title = "Mở File Sao Lưu";
            openanhdocgia.Filter = "Image Files(*.png)|*.png|(*.jpg)|*.jpg|All Files(*.*)|*.*";
            openanhdocgia.FileName = "";
            if (openanhdocgia.ShowDialog() == DialogResult.OK && openanhdocgia.FileName != "")
            {
                ptbanhdg.Image = Image.FromFile(openanhdocgia.FileName);
                
            }
            
        }
    }
}
